﻿using Architecture.API.Application.Command.MenuRoleCommand;
using Architecture.Domain.Dto.MenuRoleDto;
using Architecture.Domain.Service.MenuRoleServices;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--菜单角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class MenuRoleManagementController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public MenuRoleManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 创建菜单角色
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateMenuRole(MenuRoleCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填菜单角色MId列表
        /// </summary>
        /// <param name="command">请求参数</param>
        /// <returns>返回菜单角色RId列表</returns>
        [HttpPost]
        public async Task<ApiResult<List<MenuRoleGetIdDto>>> SearchMenuRoleMId(MenuRoleSearchMIdCommand command)
        {
            ApiResult<List<MenuRoleGetIdDto>> result = new ApiResult<List<MenuRoleGetIdDto>>();
            try
            {
                var stringRids = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;//获取Role的值(roleId);
                //command.roleId = stringRids;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
